﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_SYSTEM;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_IQLastTimeRemain : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public panel_IQLastTimeRemain()
        {
            InitializeComponent();
            LoadData();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);

            IQLastTimeRemain.PopupMenuShowing += IQLastTimeRemain_PopupMenuShowing;
            IQLastTimeRemain.DoubleClick += IQLastTimeRemain_DoubleClick;
            iqLastTimeRemainContext.ItemClicked += iqLastTimeRemainContext_ItemClicked;
        }

        void IQLastTimeRemain_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void callEditForm()
        {
            DataRow row = this.IQLastTimeRemain.GetFocusedDataRow();
            frm_Edit_IQLastTimeRemain edit_form = new frm_Edit_IQLastTimeRemain();
            edit_form.id = Int32.Parse(row["ID"].ToString());
            if (edit_form.LoadData())
            {
                edit_form.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot edit selected record");
            }
        }

        void iqLastTimeRemainContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.iqLastTimeRemainContext.Hide();
            switch (e.ClickedItem.Name)
            {
                case "addSelection":
                    frm_Add_IQLastTimeRemain add_Form = new frm_Add_IQLastTimeRemain();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "editSelection":
                    callEditForm();
                    break;
                case "deleteSelection":
                    deleteData();
                    break;             
                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void IQLastTimeRemain_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.iqLastTimeRemainContext.Show(MousePosition);            
        }
        

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT * FROM [241_IQLastTimeRemain]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridIQLastTimeRemain.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("IQID", typeof(string));
                newTable.Columns.Add("Remain", typeof(float));
                newTable.Columns.Add("LastDate", typeof(DateTime));
                gridIQLastTimeRemain.DataSource = newTable;
            }
            IQLastTimeRemain.Columns[0].Caption = "ID";
            IQLastTimeRemain.Columns[1].Caption = "IQ ID";
            IQLastTimeRemain.Columns[2].Caption = "Remain";
            IQLastTimeRemain.Columns[3].Caption = "Last Date";
            IQLastTimeRemain.Columns[0].Visible = false;
            IQLastTimeRemain.BestFitColumns();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_IQLastTimeRemain add_Form = new frm_Add_IQLastTimeRemain();
            add_Form.ShowDialog();
            LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteData();
        }

        //////////////////////////////
        /// Delete Data
        /////////////////////////////
        private void deleteData()
        {
            if (MessageBox.Show("Are you really to delete selected record now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //focusIndex = CustomClerance.GetFocusedDataSourceRowIndex();
                int[] selected_row = IQLastTimeRemain.GetSelectedRows();
                myConnection.Open();
                int k = 0;
                foreach (int i in selected_row)
                {
                    bool exist = false;
                    try
                    {
                        SqlCommand cmd_sql = myConnection.CreateCommand();
                        cmd_sql.CommandText = "SELECT * FROM [241_IQLastTimeRemain]"
                            + " WHERE ID=" + IQLastTimeRemain.GetRowCellValue(i, "ID").ToString();
                        SqlDataReader dataReader = cmd_sql.ExecuteReader();
                        if (dataReader.Read())
                        {
                            exist = true;
                        }
                        dataReader.Close();
                        cmd_sql.Dispose();
                    }
                    catch
                    {
                        exist = false;
                    }
                    if (exist)
                    {
                        try
                        {
                            string sql = "DELETE FROM [241_IQLasTimeRemain] "
                                            + "WHERE ID=" + IQLastTimeRemain.GetRowCellValue(i, "ID").ToString();
                            SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                            cmd_sql.ExecuteNonQuery();
                            cmd_sql.Dispose();
                            k++;
                        }
                        catch
                        {

                        }
                    }
                }
                myConnection.Close();
                MessageBox.Show(k + " recoreds have been deleted.");
                LoadData();
            }
        }
    }
}
